﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace WLMes
{
    public class WsHelper
    {
        /// <summary>
        /// Soap协议Post方法
        /// </summary>
        /// <param name="uri">WebService地址</param>
        /// <param name="str">传入Soap协议格式数据</param>
        /// <returns></returns>
        public async Task<string> SendToReturn(string url, int actionParam, string postData, string actionName)
        {
            string _returnstr = "";
            //发起请求
            WebRequest webRequest = WebRequest.Create(url);
            webRequest.ContentType = "text/xml;charset=utf-8";
            webRequest.Method = "POST";
            webRequest.Timeout = 10000;
            webRequest.Headers.Add("SoapAction", "https://webserviceserver.mom.siemens.com/IMOMServices/" + actionName);
            using (Stream requestStream = webRequest.GetRequestStream())
            {
                byte[] paramBytes = Encoding.UTF8.GetBytes(DataTxt(actionParam, postData, actionName));
                requestStream.Write(paramBytes, 0, paramBytes.Length);
            }
            //响应
            try
            {
                WebResponse webResponse = webRequest.GetResponse();
                using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
                {
                    _returnstr = await myStreamReader.ReadToEndAsync();
                }
            }
            catch
            {
                _returnstr = "请求失败";
            }
            return _returnstr;
        }
        /// <summary>
        /// 拼接参数
        /// </summary>
        /// <param name="actionParam"></param>
        /// <param name="postData"></param>
        /// <returns></returns>
        private string DataTxt(int actionParam, string postData, string actionName)
        {
            string str = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"https://webserviceserver.mom.siemens.com\">" +
                          "<soapenv:Header />" +
                          "<soapenv:Body>" +
                          "<web:" + actionName + ">" +
                          "<web:requestCommand>" +
                            actionParam.ToString() +
                          "</web:requestCommand>" +
                          "<web:requestData>" +
                            postData +
                          "</web:requestData>" +
                          "</web:" + actionName + ">" +
                          "</soapenv:Body>" +
                          "</soapenv:Envelope>";
            return str;
        }
        /// <summary>
        /// 获取json正文
        /// </summary>
        /// <param name="json"></param>
        /// <param name="head"></param>
        /// <returns></returns>
        public string getjson(string json, string head)
        {
            try
            {
                string inde = $"<{head}>";
                string indef = $"</{head}>";
                int ina = json.IndexOf(inde);
                int inad = json.IndexOf(indef);
                string fla = json.Substring(ina + inde.Length, inad - ina - inde.Length);
                return fla;
            }
            catch
            {
                return "";
            }
        }
    }
}
